import { RendererSystem } from '@eva/plugin-renderer'
import { Game as MyGame, LOAD_SCENE_MODE, ResourceBase, RESOURCE_TYPE } from '@eva/eva.js'
import { LoadingScreenInterface, LoadingScreen } from "./loadingScreen";
import { ImgSystem } from '@eva/plugin-renderer-img'
import { GraphicsSystem } from '@eva/plugin-renderer-graphics'
import { TransitionSystem } from '@eva/plugin-transition'
import { EventSystem } from '@eva/plugin-renderer-event'
import { GameResource } from "./resourceConst"
import GameScreen from "./gameScreen"

export default class Game {
    private loadingScreen: LoadingScreenInterface;
    private game: MyGame;
    private gameScreen: GameScreen;
    private rendererSystem: RendererSystem;
    constructor() {
        this.init();
    }

    public async init(): Promise<void> {
        // 创建渲染系统
        const rendererSystem = new RendererSystem({
            canvas: document.querySelector('#canvas'), // 可选，自动生成canvas 挂在game.canvas上
            width: 750, // 必填
            height: 1334, // 必填
            transparent: false, // 可选
            resolution: window.devicePixelRatio / 2, // 可选, 如果是2倍图设计 可以除以 2
            enableScroll: false, // 允许页面滚动
            renderType: 0 // 0:自动判断，1: WebGL，2:Canvas，建议android6.1 ios9 以下使用Canvas，需业务判断。
        })
        this.rendererSystem = rendererSystem;
        // 初始化游戏
        this.game = new MyGame({
            frameRate: 60, // 可选
            autoStart: true, // 可选
            systems: [
                rendererSystem,
                new ImgSystem(),
                new GraphicsSystem(),
                new TransitionSystem(),
                new EventSystem()
            ]
        })
        this.loadingScreen = new LoadingScreen();
        await this.loadingScreen.init();
        this.game.loadScene({
            scene: this.loadingScreen.scene,
            mode: LOAD_SCENE_MODE.SINGLE
        })

        await this.loadingScreen.loaded(GameResource);
        this.initGameScreen();

    }
    private initGameScreen() {
        this.gameScreen = new GameScreen();
        this.gameScreen.init();
        this.game.loadScene({
            scene: this.gameScreen.scene,
            mode: LOAD_SCENE_MODE.SINGLE
        })
    }
    public start() {
        this.game.start()
    }
}